package com.zjht.gmcc.servlet.provider;

import java.io.File;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator; 
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;

import com.zjht.gmcc.bean.BaseServlet;

import com.zjht.gmcc.bean.util.MyJDBC;
import com.zjht.gmcc.bean.util.MyPath;

public class FileUp extends BaseServlet {

	private static final long serialVersionUID = 2205937410942000119L;

	public boolean process(HttpServletRequest request, HttpServletResponse response, HttpSession session, PrintWriter out, MyJDBC myJDBC, String menu)
			throws Exception {
		int i_flag = 1;
		PreparedStatement ps, ps1;
		ResultSet rs;

		// ---上传图片用的参数
		// String saveDirectory =request.getRealPath("/").toString()+"//phonepic/";
		String privodeid = "";
		String saveDirectory = MyPath.PATH_PROVIDE;
		DiskFileUpload fu = null;
		File fNew = null;
		String s_msg = null; 
		String[] ii = null;
		
		String s_pictype = null; 
		String fax = null;
		String phone = null;
		String email = null;
		String firstName = "";
		int server = 0;
		String ser = "";
		String serList[] = null;
		String s_name="";
		try {

		

			fu = new DiskFileUpload();
			// 设置允许用户上传文件大小,单位:字节
			fu.setSizeMax(1 * 1024 * 1024);
			String FileName = "";
			/* 开始上传 */
			String s_picname = null;
			List fileItems = fu.parseRequest(request);
			// 读取网页传过来的参数
			String name = null;// 上传文件名称
			Iterator iter = fileItems.iterator();

			while (iter.hasNext()) {

				FileItem item = (FileItem) iter.next();
				if (!item.isFormField()) {
					// 忽略其他不是文件域的所有表单信息

					FileName = item.getName(); // 上传文件的全路径
					if (FileName != null) {
						name = FileName.substring(FileName.lastIndexOf("\\") + 1).trim();// 从全路径中提取文件名
						long size = item.getSize();
						if (name != null && (!name.equals("")) && size != 0) {
							ii = name.split("\\.");
							s_pictype = ii[ii.length - 1].toLowerCase();
							s_name = new SimpleDateFormat("yyyyMMddHmmss").format(new java.util.Date())+"."+s_pictype;
							

							if (s_pictype.equals("gif") || s_pictype.equals("jpg") || s_pictype.equals("jpeg")) {
								fNew = new File(saveDirectory, s_name);
								item.write(fNew);
								s_picname = s_name;
							} else {
								i_flag = 0;
								s_msg = "文件(" + name + ")不能上传!原因是文件过大或文件类型不对";
								break;
							}
							// 插入图片名称
						}
					}
				} else {
					if (item.getFieldName().equals("pName")) {
						
					} 
				

				}

			}
		

			if (i_flag == 1) {
				alertAndReturnValue(out, "文件上传成功!", s_name);
			} else {
				errAlert(request, out, menu, s_msg);
			}
			return i_flag == 1;

		} catch (Exception e) {
			throw e;
		} finally {

			fNew = null;
			fu = null;
		}

	}

}
